﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Kugar.Core.ExtMethod;
using Kugar.Core.Log;
using Kugar.Core.Web;
using Kugar.Web.Wechat.Entities;
using Kugar.Web.Wechat.Enums;
using Kugar.Web.WechatMP;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.AdvancedAPIs;
using HttpContext = Kugar.Core.Web.HttpContext;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Kugar.Web.Wechat.MP
{
    public class WechatMPOAuthSchemeAttribute2 : AuthorizeAttribute
    {
        public WechatMPOAuthSchemeAttribute2(string scheme, OAuthScope scope= OAuthScope.snsapi_base) : base()
        {
            this.AuthenticationSchemes = scheme;
            Scope = scope;
        }

        public OAuthScope Scope { set; get; }

    }


    /// <summary>
    /// 微信公众号授权要求
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class WechatMPOAuthSchemeAttribute: AuthorizeAttribute,IAsyncActionFilter,IActionFilter, /*IAuthorizationFilter,*/ IOrderedFilter
    {
        public WechatMPOAuthSchemeAttribute(string schemeName, 
            WechatOAuthScope scope = WechatOAuthScope.snsapi_base) : base()
        {
            if (string.IsNullOrEmpty(schemeName))
            {
                AuthenticationSchemes = HttpContext.Current.Request.Scheme;
            }
            else
            {
                AuthenticationSchemes = schemeName;
            }

            Order = 1;
            Scope=scope;
        }
        //public WechatMPOAuthSchemeAttribute(WechatOAuthScope scope = WechatOAuthScope.snsapi_base) : this(null,scope)
        //{
        //    //AuthenticationSchemes = schemeName;

        //    Scope = scope;
        //}


        public WechatOAuthScope Scope {  get; }

        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            //var controller = (Controller)context;

            //context.RouteData.Values.Add("schemename", AuthenticationSchemes);

            //var user = (WechatUserOAuth)context.HttpContext.Items["WechatMPUser"];

            //var appID = context.HttpContext.Items["AppID"].ToStringEx();

            //if (controller is IWechatMPOAuthController c1)
            //{
            //    c1.CurrentMPUser = user;
            //    c1.AppID = appID;
            //}
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var controller = (Controller)context.Controller;
            
            context.RouteData.Values.Add("schemename", AuthenticationSchemes);

            var user = (WechatUserOAuth)context.HttpContext.Items["WechatMPUser"];

            var appID = context.HttpContext.Items["AppID"].ToStringEx();

            if (controller is IWechatMPOAuthController c1)
            {
                c1.CurrentMPUser = user;
                c1.AppID = appID;
            }
            
            await next();
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var controller = (Controller)context.Controller;

            var user = (WechatUserOAuth)context.HttpContext.Items["WechatMPUser"];

            context.HttpContext.Items["schemename"] = AuthenticationSchemes;

            var appID = context.HttpContext.Items["AppID"].ToStringEx();

            if (controller is IWechatMPOAuthController c1)
            {
                c1.CurrentMPUser = user;
                c1.AppID = appID;
            }
        }

        

        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }

        public int Order { get; }
        //public void OnAuthorization(AuthorizationFilterContext context)
        //{
        //    context.
        //}
    }
    
}
